package com.byq.mm.common.engine;

import java.util.Collection;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.roster.Roster;

import com.byq.mm.abstractx.engine.ExtensionManager;

import com.byq.mm.abstractx.protocol.Defination;
import com.byq.mm.abstractx.protocol.Interaction;
import com.google.gson.Gson;

public class XmppExtensionManager implements ExtensionManager {
	private static final String String = null;
	Environment env;

	public XmppExtensionManager(Environment env) {
		super();
		this.env = env;
	}

	boolean checkTransport(){
		if(!env.getConnectionManager().isLoggedIn()){
			env.getEventCenter().notifyTransportClosed();
			return false;
		}
		return true;
	}
	boolean checkAvailability(String to){
		XMPPConnection con = env.getConnection();

        Presence p = null;

            p = Roster.getInstanceFor(con).getPresence(to);


        boolean rst =  (p.getType() == Presence.Type.available);
		if(!rst)
			env.getEventCenter().notifyUnavailable(to);
		return rst;
	}
	public void sendDefination(Defination def, String jid) {
	/*	if(!checkTransport())
			return;
		if(!checkAvailability(jid))
			return;

	 */


        final Message m = new Message(jid,Message.Type.chat);
		m.setSubject("defination");
		Gson gson = new Gson();
		String json = gson.toJson(def);
		m.setBody(json);

		
		new Thread() {
			@Override
			public void run() {
				if (env.getConnection() != null && env.getConnection().getUser() != null) {
                    try {
                        env.getConnection().sendStanza(m);
                    } catch (SmackException.NotConnectedException e) {
                        throw new RuntimeException(e);
                    }

                }
			}
		}.start();
	}

	public void sendInteraction(Interaction i, String to) {

		final Message m = new Message(to,Message.Type.chat);
		m.setSubject("interaction");
		Gson gson = new Gson();
		String json = gson.toJson(i);
		m.setBody(json);

		new Thread() {
			@Override
			public void run() {
				if (env.getConnection() != null
						&& env.getConnection().getUser() != null) {
                    try {
                        env.getConnection().sendStanza(m);
                    } catch (SmackException.NotConnectedException e) {
                        throw new RuntimeException(e);
                    }

                }

			}
		}.start();
	}

	public void broadcastInteraction(final Interaction i,final Collection<String> list) {
		if(!checkTransport())
			return;



		Gson gson = new Gson();
		String json = gson.toJson(i);


		new Thread() {
			@Override
			public void run() {
				env.getEventCenter().loopPrepare();
				if (env.getConnection() != null
						&& env.getConnection().getUser() != null) {
					for(String to : list){
						if(!checkAvailability(to))
							continue;
						Message m= new Message(to);
						m.setSubject("interaction");
						m.setBody(json);

                        try {
                            env.getConnection().sendStanza(m);
                        } catch (SmackException.NotConnectedException e) {
                            throw new RuntimeException(e);
                        }
                    }

				}

			}
		}.start();
		
	}
}
